;
(function (window, angular) {
    'use strict';
    angular.module("login.manager", ["ui.router", "ui.bootstrap", "angular.filter", "YB.Widget", "toggle-switch", "toaster", "Directive.All", "Service.All", "Filter.All", "template-directive", "template-widget"])
        .run(["$document", "$rootScope", "ybConfig", "$state", function ($document, $rootScope, ybConfig, $state) {
            ybConfig.initConfig();
            $state.go("login");
        }])

        .controller("LoginCtrl", ['$scope', function ($scope) {
            $scope.sysName = window.AppData.config.otherData.sysName;
        }])

        .config(['$stateProvider', "$logProvider", function ($stateProvider, $logProvider) {
            $logProvider.debugEnabled(true);

            $stateProvider.state("login", {
                url: "/login",
                templateUrl: "view/login/login.html",
                controller: "LoginPageCtrl"
            });
        }])

        .controller("LoginPageCtrl", ['$scope', '$window', "UserContextService", 'ybHttp', 'ybWidget', 'ybConfig', function ($scope, $window, UserContextService, ybHttp, ybWidget, ybConfig) {
            $scope.failLogin = false;
            $scope.showFormError = false;
            $scope.user = {"loginName": "", "password": "", code: ""};
            $scope.resetMessage = function () {
                $scope.failLogin = false;
            };
            $scope.codeImg = '';
            $scope.xcmg_version = '';
            var authId = '';

            function getAuthCode() {
                var service = "auth";
                var reqUrl = "/auth/authInfo";
                ybHttp.Get(service, reqUrl).then(function (data) {
                    if(data.code != 0){
                        ybWidget.ShowMessage(data.message);
                        return;
                    }
                    authId = data.value.authId;
                    $scope.codeImg = ybConfig.getConfig().serviceName.auth + '/auth/kaptcha?authid=' + data.value.authId;
                })
            }

            function getVersion() {
                var service = "device";
                var reqUrl = "/dataDictionary/version";
                ybHttp.Get(service, reqUrl).then(function (resp) {
                    $scope.xcmg_version = resp;
                });
            }

            $scope.login = function (isValid) {
                if (!isValid) {
                    $scope.showFormError = true;
                    return;
                }
                $scope.showFormError = false;
                $scope.user.authId = authId;
                var result = UserContextService.Login($scope.user, true);
                result.then(function (data) {
                    $scope.failLogin = false;
                    $window.location.href = "main.html";
                },
                function (data) {
                    $scope.failLogin = true;
                    $scope.Message = data;
                    $scope.user.Password = "";
                    $scope.refresh();
                });
            };

            $scope.refresh = function () {
                getAuthCode();
            };

            (function () {
                getAuthCode();
                getVersion();
            })();
        }])

    ;
})(window, window.angular);